package com.ydd.app.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ydd.app.bean.common.CompanyInfoBean;
import com.ydd.app.bean.common.CompanyTypeBean;
import com.ydd.app.bean.common.ConfigTypeBean;
import com.ydd.app.bean.input.CreateCompanyRequest;
import com.ydd.app.bean.input.CreateConfigTypesRequest;
import com.ydd.app.bean.input.QueryCompaniesRequest;
import com.ydd.app.bean.input.QueryCompanyRequest;
import com.ydd.app.bean.input.QueryCompanyTypesRequest;
import com.ydd.app.bean.input.QueryConfigTypesRequest;
import com.ydd.app.bean.input.QueryNewestCompaniesRequest;
import com.ydd.app.bean.input.UpdateDisplayFieldRequest;
import com.ydd.app.bean.output.CreateCompanyResponse;
import com.ydd.app.bean.output.CreateConfigTypesResponse;
import com.ydd.app.bean.output.QueryCompaniesResponse;
import com.ydd.app.bean.output.QueryCompanyResponse;
import com.ydd.app.bean.output.QueryCompanyTypesResponse;
import com.ydd.app.bean.output.QueryConfigTypesResponse;
import com.ydd.app.bean.output.QueryNewestCompaniesResponse;
import com.ydd.app.bean.output.UpdateDisplayFieldResponse;
import com.ydd.app.manager.CompanyMng;
import com.ydd.common.util.CommonUtil;

@Controller
public class CompanyAct extends CommonAct{
	private static final Logger  log = Logger.getLogger(CompanyAct.class);

	/**
	 * =============
	 * 获取项目列表
	 * =============
	 */
	@RequestMapping(value = "/queryCompanies.do", method = {RequestMethod.GET,RequestMethod.POST})
	public String queryCompanies(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + BEGIN_METHOD);
		// 接口返回配置
		String callback = request.getParameter(CALLBACK);
		QueryCompaniesResponse ret = new QueryCompaniesResponse();		
		try{
			// 参数获取
			QueryCompaniesRequest input = new QueryCompaniesRequest();	
			getParameterBean(request, input);


			final String pageCount = msg.getValue("companiesCount");
			if ( CommonUtil.isNotEmpty(pageCount)) {
				input.setPageCount(Integer.parseInt(pageCount));
			}
			
			
			final CompanyInfoBean[] companies = _companyMng.queryCompanyInfos(input.getCompanyTypeId(),
					input.getPage(), input.getPageCount());
			if( null != companies ) {
				ret.setCode(STATIC_RET_OK);
				ret.setSuccess(true);
				ret.setCompanies(companies);
				ret.setRowCount(_companyMng.queryCompanyCount(input.getCompanyTypeId()));
			}
			else {
				ret.setCode(STATIC_RET_OK);
				ret.setCompanies(new CompanyInfoBean[0]);
			}
			
			log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + END_METHOD);
		} catch(Exception e){
			// 接口执行失败
			ret.setCode(STATIC_RET_ERROR_1000);
			ret.setSuccess(false);
			ret.setMessage(msgUtils.getValue("系统异常"));
			
			log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + e.getMessage());
		}
		
		// BEAN转换为JSON数据格式
		CommBeanToJson(ret,response,callback);
		
		return RESULTAPP;
	}
	
	/**
	 * =============
	 * 获取项目详细
	 * =============
	 */
	@RequestMapping(value = "/queryCompany.do", method = {RequestMethod.GET,RequestMethod.POST})
	public String queryCompany(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + BEGIN_METHOD);
		// 接口返回配置
		String callback = request.getParameter(CALLBACK);
		QueryCompanyResponse ret = new QueryCompanyResponse();		
		try{
			// 参数获取
			QueryCompanyRequest input = new QueryCompanyRequest();	
			getParameterBean(request, input);
			
			final CompanyInfoBean company = _companyMng.queryCompanyInfo(input.getCompanyId(), input.getUserId());
			if( null != company ) {
				ret.setCode(STATIC_RET_OK);
				ret.setCompany(company);
				ret.setSuccess(true);
			}
			else {
				ret.setCode(STATIC_RET_ERROR_2000);
				ret.setMessage("未找到该资料");
			}

			log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + END_METHOD);
		} catch(Exception e){
			// 接口执行失败
			ret.setCode(STATIC_RET_ERROR_1000);
			ret.setSuccess(false);
			ret.setMessage(msgUtils.getValue("系统异常"));
			
			log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + e.getMessage());
		}
		
		// BEAN转换为JSON数据格式
		CommBeanToJson(ret,response,callback);
		
		return RESULTAPP;
	}
	
	/**
	 * =============
	 * 获取最新项目列表
	 * =============
	 */
	@RequestMapping(value = "/queryNewestCompanies.do", method = {RequestMethod.GET,RequestMethod.POST})
	public String queryNewestCompanies(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + BEGIN_METHOD);
		// 接口返回配置
		String callback = request.getParameter(CALLBACK);
		QueryNewestCompaniesResponse ret = new QueryNewestCompaniesResponse();		
		try{
			// 参数获取
			QueryNewestCompaniesRequest input = new QueryNewestCompaniesRequest();	
			getParameterBean(request, input);
			final ConfigTypeBean[] companyTypes = _companyMng.queryNewestCompanyInfos(
					CommonUtil.isNotEmpty(msg.getValue("newescompaniesCount")) ? 
							Integer.parseInt(msg.getValue("newescompaniesCount")) : 3);
			if( null != companyTypes ) {
				ret.setCode(STATIC_RET_OK);
				ret.setCompanyTypes(companyTypes);
			}
			else {
				ret.setCode(STATIC_RET_OK);
				ret.setCompanyTypes(new ConfigTypeBean[0]);
			}

			log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + END_METHOD);
		} catch(Exception e){
			// 接口执行失败
			ret.setCode(STATIC_RET_ERROR_1000);
			ret.setSuccess(false);
			ret.setMessage(msgUtils.getValue("系统异常"));
			
			log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + e.getMessage());
		}
		
		// BEAN转换为JSON数据格式
		CommBeanToJson(ret,response,callback);
		
		return RESULTAPP;
	}
	
	/**
	 * =============
	 * 获取项目分类 (已弃用)
	 * =============
	 */
	@RequestMapping(value = "/queryCompanyTypes.do", method = {RequestMethod.GET,RequestMethod.POST})
	public String queryCompanyTypes(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + BEGIN_METHOD);
		// 接口返回配置
		String callback = request.getParameter(CALLBACK);
		QueryCompanyTypesResponse ret = new QueryCompanyTypesResponse();		
		try{
			// 参数获取
			QueryCompanyTypesRequest input = new QueryCompanyTypesRequest();	
			getParameterBean(request, input);
			final CompanyTypeBean[] companies= _companyMng.queryCompanyTypes();
			if( null != companies ) {
				ret.setCode(STATIC_RET_OK);
				ret.setCompanyTypes(companies);
			}
			else {
				ret.setCode(STATIC_RET_OK);
				ret.setCompanyTypes(new CompanyTypeBean[0]);
			}

			log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + END_METHOD);
		} catch(Exception e){
			// 接口执行失败
			ret.setCode(STATIC_RET_ERROR_1000);
			ret.setSuccess(false);
			ret.setMessage(msgUtils.getValue("系统异常"));
			
			log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + e.getMessage());
		}
		
		// BEAN转换为JSON数据格式
		CommBeanToJson(ret,response,callback);
		
		return RESULTAPP;
	}
	
	/**
	 * =============
	 * 获取行业列表
	 * =============
	 */
	@RequestMapping(value = "/queryConfigTypes.do", method = {RequestMethod.GET,RequestMethod.POST})
	public String queryConfigTypes(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + BEGIN_METHOD);
		// 接口返回配置
		String callback = request.getParameter(CALLBACK);
		QueryConfigTypesResponse ret = new QueryConfigTypesResponse();		
		try{
			// 参数获取
			QueryConfigTypesRequest input = new QueryConfigTypesRequest();	
			getParameterBean(request, input);
			final ConfigTypeBean[] types= _companyMng.queryConfigTypes();
			if( null != types ) {
				ret.setCode(STATIC_RET_OK);
				ret.setConfigTypes(types);
			}
			else {
				ret.setCode(STATIC_RET_OK);
				ret.setConfigTypes(new ConfigTypeBean[0]);
			}

			log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + END_METHOD);
		} catch(Exception e){
			// 接口执行失败
			ret.setCode(STATIC_RET_ERROR_1000);
			ret.setSuccess(false);
			ret.setMessage(msgUtils.getValue("系统异常"));
			
			log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + e.getMessage());
		}
		
		// BEAN转换为JSON数据格式
		CommBeanToJson(ret,response,callback);
		
		return RESULTAPP;
	}
	
	/**
	 * =============
	 * 获取行业列表
	 * =============
	 */
	@RequestMapping(value = "/createCompany.do", method = {RequestMethod.GET,RequestMethod.POST})
	public String createCompany(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + BEGIN_METHOD);
		// 接口返回配置
		String callback = request.getParameter(CALLBACK);
		CreateCompanyResponse ret = new CreateCompanyResponse();		
		try{
			// 参数获取
			CreateCompanyRequest input = new CreateCompanyRequest();	
			getParameterBean(request, input);
			if( _companyMng.createCompany(input) ) {
				ret.setCode(STATIC_RET_OK);
				ret.setSuccess(true);
			}
			else {
				ret.setCode(STATIC_RET_ERROR_2000);
				ret.setMessage("创建失败");
			}

			log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + END_METHOD);
		} catch(Exception e){
			// 接口执行失败
			ret.setCode(STATIC_RET_ERROR_1000);
			ret.setSuccess(false);
			ret.setMessage(msgUtils.getValue("系统异常"));
			
			log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + e.getMessage());
		}
		
		// BEAN转换为JSON数据格式
		CommBeanToJson(ret,response,callback);
		
		return RESULTAPP;
	}
	
	/**
	 * =============
	 * 创建行业列表（同步）
	 * =============
	 */
	@RequestMapping(value = "/createConfigTypes.do", method = {RequestMethod.GET,RequestMethod.POST})
	public String createConfigTypes(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + BEGIN_METHOD);
		// 接口返回配置
		String callback = request.getParameter(CALLBACK);
		CreateConfigTypesResponse ret = new CreateConfigTypesResponse();		
		try{
			// 参数获取
			CreateConfigTypesRequest input = new CreateConfigTypesRequest();	
			getParameterBean(request, input);
			if( _companyMng.createCOnfigType(input)) {
				ret.setCode(STATIC_RET_OK);
				ret.setSuccess(true);
			}
			else {
				ret.setCode(STATIC_RET_ERROR_2000);
				ret.setMessage("创建失败");
			}

			log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + END_METHOD);
		} catch(Exception e){
			// 接口执行失败
			ret.setCode(STATIC_RET_ERROR_1000);
			ret.setSuccess(false);
			ret.setMessage(msgUtils.getValue("系统异常"));
			
			log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + e.getMessage());
		}
		
		// BEAN转换为JSON数据格式
		CommBeanToJson(ret,response,callback);
		
		return RESULTAPP;
	}
	
	/**
	 * =============
	 * 创建行业列表（同步）
	 * =============
	 */
	@RequestMapping(value = "/updateDisplayField.do", method = {RequestMethod.GET,RequestMethod.POST})
	public String updateDisplayField(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + BEGIN_METHOD);
		// 接口返回配置
		String callback = request.getParameter(CALLBACK);
		UpdateDisplayFieldResponse ret = new UpdateDisplayFieldResponse();		
		try{
			// 参数获取
			UpdateDisplayFieldRequest input = new UpdateDisplayFieldRequest();	
			getParameterBean(request, input);
			if( _companyMng.updateDisplayField(input)) {
				ret.setCode(STATIC_RET_OK);
				ret.setSuccess(true);
			}
			else {
				ret.setCode(STATIC_RET_ERROR_2000);
				ret.setMessage("创建失败");
			}

			log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + END_METHOD);
		} catch(Exception e){
			// 接口执行失败
			ret.setCode(STATIC_RET_ERROR_1000);
			ret.setSuccess(false);
			ret.setMessage(msgUtils.getValue("系统异常"));
			
			log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + e.getMessage());
		}
		
		// BEAN转换为JSON数据格式
		CommBeanToJson(ret,response,callback);
		
		return RESULTAPP;
	}
	
	@Autowired
	private CompanyMng _companyMng;

}
